<template>
  <view>
    <customtop top_title="公司主页"></customtop>
    <view class="company_show" v-if="!loading">
      <view class="">
        <view class="pagebox top_page">
          <image mode="aspectFill" :src="company.logo" class="cover" />
          <view class="name">{{company.shortname}}</view>
          <view class="types">
            <span class="type">{{company.name}}</span>
          </view>
          <view class="types">
            <span class="type">{{company.industry}}行业</span>
          </view>
          <view class="actions">
            <view class="btn collect" v-if="company.collect == 0" @click="collect"><i class="iconfont icon-shoucang"></i><span>收藏</span></view>
            <view class="btn collect" v-if="company.collect == 1" @click="collect"><i class="iconfont icon-shoucang-shoucang"></i><span>已收藏</span></view>
          </view>
        </view>
        <view class="pagebox user_list2">
          <view class="stitle"><span class="txt">公司成员</span></view>
          <view class="bd">
            <view class="items">
              <view class="item" v-for="(item, index) in users" :key="index" @click="jumpPage('/pages/user/show?id=' + item.id)">
                <image mode="aspectFill" :src="item.avatar" class="cover" />
                <view class="info">
                  <view class="name">{{item.nickname}}</view>
                  <view class="types">
                    <view class="types_item">
                      <span>{{item.city_shortname}}</span>
                    </view>
                    <view class="types_item">
                      <span>{{item.job_title}}</span>
                    </view>
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
        <view class="pagebox post_list2">
          <view class="stitle"><span class="txt">发布记录</span></view>
          <view class="bd" v-if="!data_list_loading">
            <view v-if="data_list.length > 0">
              <view class="items">
                <view class="item" v-for="(item, index) in data_list" :key="index" @click="jumpPage('/pages/post/show?id=' + item.id)">
                  <view class="top">
                    <view class="title">{{item.title}}</view>
                  </view>
                  <view class="types">
                    <view class="type_item">
                      <span>编号：</span><span>{{item.id}}</span>
                    </view>
                    <view class="type_item">
                      <span>类目：</span><span>{{item.category_name}}</span>
                    </view>
                  </view>
                  <view class="item_foot">
                    <view class="user_info">
                      <view class="avatar"><image class="img" :src="item.user_avatar"></view>
                      <view class="txtcontent">
                        <span class="name">{{item.user_nickname}}</span>
                      </view>
                    </view>
                  </view>
                </view>
              </view>
              <view class="uloadmore" @click="getMore"><u-loadmore :status="loadmore_status" /></view>
            </view>
            <u-empty
              v-if="data_list.length == 0"
              mode="data"
              icon="http://cdn.uviewui.com/uview/empty/data.png"
              text="暂无内容~"
            >
            </u-empty>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { request } from "@/utils/http.js"
import customtop from "@/components/customtop.vue"
export default {
  components: { customtop },

  data() {
    return {
      loading: true,
      company: {},
      users: [],
      data_list: [],
      data_list_loading: true,
      loadmore_status: 'loadmore',
      loadmore_finished: false,
      params: {
        page_size: 15,
        page: 1,
      },
    }
  },

  onLoad(options) {
    uni.showLoading();
    this.id = options.id;
    this.getCompany();
    this.getUsers();
    this.getPostsPaginate();
  },

  onReachBottom() {
    this.getMore();
  },

  methods: {
    getCompany: function() {
      request.post('/company/getCompany', {id: this.id}).then(res => {
        uni.hideLoading();
        this.loading = false;
        this.company = res.data;
      })
    },

    getUsers: function() {
      request.post('/user/getUsers', {company_id: this.id}).then(res => {
        uni.hideLoading();
        this.users = res.data;
      })
    },

    getPostsPaginate: function() {
      let params = this.params;
      params.company_id = this.id;
      request.post('/post/getPostsPaginate', params).then(res => {
        uni.stopPullDownRefresh();
        uni.hideLoading();
        this.data_list_loading = false;

        // 返回数据为空
        if (res.data.total == 0) {
          this.data_list = [];
          return false;
        }

        // 组装数据
        if (res.data.current_page == 1) {
          this.data_list = res.data.data;
        } else {
          this.data_list = this.data_list.concat(res.data.data);
        }

        // 最后一页
        if (this.params.page == res.data.last_page) {
          this.loadmore_finished = true;
          this.loadmore_status = 'nomore';
          return false;
        }

        this.params.page = parseInt(res.data.current_page) + parseInt(1);
        this.loadmore_status = 'loadmore';
        this.loadmore_finished = false;
      })
    },

    getMore: function() {
      if (!this.loadmore_finished) {
        this.loadmore_status = 'loading';
        this.getPostsPaginate();
      }
    },

    // 收藏
    collect: function() {
      uni.showLoading();
      let params = {};
      params.company_id = this.id;
      request.post('/company/collect', params).then(res => {
        uni.hideLoading();
        if (res.code == 200) {
          let title = '已收藏';
          if (res.data != 1) title = '已取消收藏';
          uni.showToast({
            title: title,
            icon: 'none'
          })
          this.company.collect = res.data;
        } else if (res.code == 400) {
          uni.showToast({
            title: res.message,
            icon: 'none'
          })
          return false;
        }
      })
    },

    switchTab: function(url) {
      uni.switchTab({ url: url });
    },

    jumpPage: function(url) {
      uni.navigateTo({ url: url });
    }
  }
}
</script>

<style>
@import url("company.css");
</style>
